#include<bits/stdc++.h>
#define ll long long
#define MOD 998244353
#define REP(i,a,n) for(int i=(a);i<(int)(n);++i)
#define pb push_back
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define deal(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define lbound(v,x) (lower_bound(all(v),x)-v.begin())
using namespace std;
int n,q,k;
int a[100005],qr[100005],A[100005];
bool D[20][100005];
int dep[400005];
int op[400005],b[400005];
void build(int l,int r,int d,int p){
    dep[p]=d;b[p]=D[d][l>>d];
    if(l==r){
        if(l<n)op[p]=l;
        else op[p]=-1;
        return;
    }
    int m=(l+r)>>1;
    build(l,m,d-1,p*2+1);build(m+1,r,d-1,p*2+2);
    int x=op[p*2+1],y=op[p*2+2];
    if(x==-1&&y==-1)op[p]=-1;
    else if(x==-2||y==-2)op[p]=-2;
    else if(x>=0&&y>=0){
        if(!b[p])op[p]=a[x]>=d? x:y;
        else op[p]=a[y]>=d? y:x;
    }else op[p]=-2;
}
struct winners{
    set<pii>a;
    int b;ll s;
    void init(){
        b=s=0;a.clear();
    }
    void add(int l,int r){
        b+=r-l+1;s+=1ll*(r-l+1)*(r+l+2)/2;
    }
    void Add(int x){
        a.insert({::a[x],x});
    }
    ll calc(){
        ll S=s;
        for(auto i:a)S+=i.second+1;
        return S;
    }
    void cl(int x){
        while(a.size()&&a.begin()->first<x)a.erase(a.begin());
    }
    bool check(int x){
        return b||(*a.begin()).first<x;
    }
};
winners res;
void solve(int l,int r,int d,int p,int N){
    if(r<=N){
        res.init();res.Add(op[p]);
        return;
    }
    if(l>N){
        res.init();res.add(l,r);
        return;
    }
    int m=(l+r)>>1;
    if(N==m){
        res.init();
        if(b[p]){
            res.Add(op[p*2+1]);
            res.add(m+1,r);
        }else if(a[op[p*2+1]]>=d)res.Add(op[p*2+1]);
        else res.add(m+1,r);
        return;
    }
    if(m>N){
        solve(l,m,d-1,p*2+1,N);
        if(b[p])res.add(m+1,r);
        else{
            if(!res.check(d))return;
            res.cl(d);
            res.add(m+1,r);
        }
    }else{
        solve(m+1,r,d-1,p*2+2,N);
        if(!b[p]){
            if(a[op[p*2+1]]>=d){
                res.init();
                res.Add(op[p*2+1]);
            }
        }else{
            if(!res.check(d))return;
            res.cl(d);
            res.Add(op[p*2+1]);
        }
    }
}
int Tc;
void Main(){
    int r[4];
    REP(i,0,4)cin>>r[i];
    REP(i,0,n)a[i]=min(A[i]^r[(i+1)%4],k);
    build(0,(1<<k)-1,k,0);
    ll ans=0;
    REP(_,0,q){
        int x=qr[_],p=0,d=k;
        if(!x){
            ans^=(_+1);
            continue;
        }
        while(x<(1<<(d-1)))p=p*2+1,--d;
        solve(0,(1<<d)-1,d,p,x);
        ans^=1ll*(_+1)*res.calc();
    }
    cout<<ans<<endl;
}
signed main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int tc=1;
    cin>>n>>q;
    REP(i,0,n)cin>>A[i];
    k=1;
    while((1<<k)<n)++k;
    REP(i,0,q)cin>>qr[i],--qr[i];
    REP(i,0,k){
        string s;
        cin>>s;
        REP(j,0,(1<<(k-i-1)))D[i+1][j]=s[j]=='1';
    }
    cin>>tc;//q=1;
    while(tc--)Main();
    return 0;
}